#include "CreateConeWidget.h"

#include "Core/MainWindow.h"
#include <gp_Pnt.hxx>
#include <gp_Ax2.hxx>
#include <QMessageBox>
#include "FeatureShape/FeatureShape.h"

CreateConeWidget::CreateConeWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateSolidWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateConeWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreateConeWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateConeWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateConeWidget::onApply);

	connect(ui->radiusDown, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onSizeChanged);
	connect(ui->radiusUp, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onSizeChanged);
	connect(ui->height, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onSizeChanged);

	connect(ui->pX, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onPosChanged);
	connect(ui->pY, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onPosChanged);
	connect(ui->pZ, &QDoubleSpinBox::valueChanged, this, &CreateConeWidget::onPosChanged);

	connect(ui->selectCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CreateConeWidget::OnComboBoxIndexChanged);
	m_originPoint = new RWTripleFloatInput(ui->pX, ui->pY, ui->pZ);

	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeCone* ConeShape = static_cast<FeatureShapeCone*>(CreateOP->m_featureShape.get());
		ui->radiusDown->setValue(ConeShape->_radius1);
		ui->radiusUp->setValue(ConeShape->_radius2);
		ui->height->setValue(ConeShape->_height);
		m_originPoint->SetValue(ConeShape->_position);
	}

	cacheActor = m_mainWindow->createCone(ui->radiusDown->value(), ui->radiusUp->value(), ui->height->value());
	onPosChanged(0.f);
}

CreateConeWidget::~CreateConeWidget()
{
	delete ui;
}

void CreateConeWidget::onSizeChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	// 这里可能不是value真的发生了变化
	if (ui->radiusDown->value() <= ui->radiusUp->value())
	{
		// 创建并弹出一个警告对话框
		QMessageBox::warning(this, "警告", "底半径必须大于上半径!!!");
		return;
	}
	cacheActor = m_mainWindow->UpdateConeActor(cacheActor, ui->radiusDown->value(), ui->radiusUp->value(), ui->height->value());
	onPosChanged(0.f);
}

void CreateConeWidget::onPosChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	cacheActor = m_mainWindow->UpdateActorPosition(cacheActor, gp_Pnt(ui->pX->value(), ui->pY->value(), ui->pZ->value()));
}

ShapeOperation* CreateConeWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("圆锥"), cacheActor->m_featureShape);
	return op;
}

void CreateConeWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeCone(cacheActor->OccShape(), ui->radiusDown->value(), ui->radiusUp->value(), ui->height->value()));
}

void CreateConeWidget::Reset()
{
	QRWCreateSolidWidget::Reset();

	m_originPoint->SetValue(gp_Pnt());
	ui->radiusDown->setValue(1.f);
	ui->radiusUp->setValue(0.f);
	ui->height->setValue(1.f);

	cacheActor = m_mainWindow->createCone(ui->radiusDown->value(), ui->radiusUp->value(), ui->height->value());
	onPosChanged(0.f);
}

